package com.qf.openapi.web.master.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //验证用户是否登录过
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if (user == null) {
            // 重定向到登录页面
            response.sendRedirect("/login.html");
            // 不再把请求发送给后续的controller
            return false;
        }

        return true;
    }
}
